/*
    Text.cpp

    Date:
        05-09-10

    Descripton:
        An object for Text items.

    Authors:
        Nathaniel Brown
*/


/****************************************************************************/
/*                              I N C L U D E S                             */
/****************************************************************************/

#include "Text.h"
#include "Theme.h"

/****************************************************************************/
/*                               E X T E R N S                              */
/****************************************************************************/

extern Theme thm;

/****************************************************************************/
/*             T E X T   :   M E M B E R   F U N C T I O N S            */
/****************************************************************************/

Text::Text() {    
    value = "";
    x = 0;
    y = 0;
}

Text::Text(string v, int ex, int why) {    
    value = v;
    x = ex;
    y = why;
}
/*
bool
Text::draw() {
    glColor4f(thm.descriptionBackColor.r, thm.descriptionBackColor.g, thm.descriptionBackColor.b, thm.descriptionBackColor.t);
    glRecti(x-10, y-15, x+200, y+100);
    glColor4f(thm.descriptionTextColor.r, thm.descriptionTextColor.g, thm.descriptionTextColor.b, thm.descriptionTextColor.t);
    glRasterPos2f(x+184, y-15);
    for(unsigned int i = 0; i < value.length(); i++) {
        if(i>150) {
            glutBitmapCharacter(GLUT_BITMAP_8_BY_13, '.');
            glutBitmapCharacter(GLUT_BITMAP_8_BY_13, '.');
            glutBitmapCharacter(GLUT_BITMAP_8_BY_13, '.');
            return false;
        }
        if(i==0) {
            glBitmap(0,0,0,0,-184,-15,0);
            glutBitmapCharacter(GLUT_BITMAP_8_BY_13, value[i]);
            continue;
        }
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, value[i]);
        if(i % 22 == 0) {
            i++;
            if(value[i] == ' ' || value[i-1] == ' ') {
                glutBitmapCharacter(GLUT_BITMAP_8_BY_13, ' ');
            }
            else{
                glutBitmapCharacter(GLUT_BITMAP_8_BY_13, '-');
            } 
            glBitmap(0,0,0,0,-184,-15,0);
            glutBitmapCharacter(GLUT_BITMAP_8_BY_13, value[i]);
        }
    }
    return true;
}
*/
bool
Text::draw() {
    int startX = x + 10;
    int currY  = y + 15;
    int endX   = x + 250;
    int endY   = y + 150;
    
    int currX = startX;
    int XLen = 8;
    int thisLineI = 0;
    
    glColor4f(thm.descriptionBackColor.r, thm.descriptionBackColor.g, thm.descriptionBackColor.b, thm.descriptionBackColor.t);
    glRecti(x, y, endX + 10, endY + 15);
    glColor4f(thm.descriptionTextColor.r, thm.descriptionTextColor.g, thm.descriptionTextColor.b, thm.descriptionTextColor.t);
    glRasterPos2f(startX, currY);
    
    for(int i = 0; i < value.length(); ) {        
        if(value[i] == '\n') { // if it is a newline then just go to a new line...
            currY += 15;
            if(currY >= endY - 15) return false; // return if run out of room.
            glRasterPos2f(startX, currY);
            currX = startX;
            thisLineI = 0;
            i++;
        }
        
        int nextSpace = 0;
        for(int j = i; j < value.length();j++) { // find next space
            if(value[j] == ' ') {
                nextSpace = j;
                break;
            }
        }
        
        if((nextSpace - i + thisLineI) * XLen + startX > endX) { // newline next word
            currY += 15;
            if(currY >= endY - 15) return false; // return if run out of room.
            glRasterPos2f(startX, currY);
            currX = startX;
            thisLineI = 0;
            for( ; i <= nextSpace; i++) { // print word
                glutBitmapCharacter(GLUT_BITMAP_8_BY_13, value[i]);
                currX += XLen;
                thisLineI++;
            }
        }
        else{ // next character this line
            for( ; i <= nextSpace; i++) { // print word
                glutBitmapCharacter(GLUT_BITMAP_8_BY_13, value[i]);
                currX += XLen;
                thisLineI++;
            }
        }
    }
    return true;
}

bool
Text::drawMenuText() {
    glColor4f(thm.faceColor.r, thm.faceColor.g, thm.faceColor.b, thm.faceColor.t);
    glRecti(x, y, x + (value.length() * 9) + 23, y + 15);
    glColor4f(thm.tickColor.r, thm.tickColor.g, thm.tickColor.b, thm.tickColor.t);
    glRasterPos2f(x + 12, y + 12);
    for(unsigned int i = 0; i < value.length(); i++) {
        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, value[i]);
    }
    return true;
}

bool
Text::drawPlainText() {
    glColor4f(thm.faceColor.r, thm.faceColor.g, thm.faceColor.b, thm.faceColor.t);
    glRasterPos2f(x, y);
    for(unsigned int i = 0; i < value.length(); i++) {
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, value[i]);
    }
    return true;
}

bool
Text::positionIsInMenu(int _x, int _y) {
    int x2 = (x + value.length() * 15) + 20;
    int y2 = (y + 15) + 20;
    if (   _x >= x 
        && _y >= y
        && _x <= x2
        && _y <= y2 ) {
        return true;
    }
    return false;
}